﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplicationRating.DB.model;

namespace WebApplicationRating.Views
{
    public partial class Categories : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {




        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                if (e.Exception.InnerException.Message.Substring(0, 61) == "The DELETE statement conflicted with the REFERENCE constraint")
                {
                    lblException.Text = "Images are Assigned, Cannot Delete. Contact Support for More Information.";
                }
                else
                {
                    lblException.Text = "Contact Support. " + e.Exception.InnerException.Message.Substring(0, 61);
                }

                e.ExceptionHandled = true;
            }
            else if (e.AffectedRows != 0)
            {
                //Display Success Message that Record Deleted Successfully 
            }

            else
            {
                lblException.Text = "Could not Perform Delete. Contact Support.";
            }

        } 


        protected void NewCategoryButton_Click(object sender, EventArgs e)
        {
            string strCategoryName = categoryName.Text.ToString();
            if (strCategoryName != null &&
                strCategoryName != "")
            {
                //TODO check to see if it already exists...
                using (imageEntities ctx = new imageEntities())
                {
                    Category newCategory = new Category();
                    newCategory.category_name = strCategoryName;
                    //System.Console.WriteLine(strCategoryName);
                    ctx.AddToCategories(newCategory);
                    ctx.SaveChanges();
                    Response.Redirect("Categories.aspx");
                }
            }
        }
    }
}